import { FormSchema } from '@/components/Form';
import { BasicColumn } from '@/components/Table';
import { DescItem } from '@/components/Description';
import { dicDictList } from '@/api/sys/dict.api';
import { dictConversion } from '@/utils/xueyi';
import { h } from 'vue';
import { Tinymce } from '@/components/Tinymce';
import { OpFaultProcessIM } from '@/model/operations/faultprocess.model';
import { Tag } from 'ant-design-vue';

/** 字典查询 */
export const dictMap = await dicDictList([
    'sys_priority',
]);

/** 字典表 */
export const dict: any = {
  DicPriorityOptions: dictMap['sys_priority'],
};

/** 表格数据 */
export const columns: BasicColumn[] = [
  {
    title: '标题',
    dataIndex: 'title',
    width: 220,
  },
  {
    title: '工单状态',
    dataIndex: 'faultProcessStatus',
    width: 220,
  },
  {
    title: '优先级',
    dataIndex: 'priority',
    customRender: ({ record }) => {
      const data = record as OpFaultProcessIM;
      return dictConversion(dict.DicPriorityOptions, data.priority);
    },
    width: 220,
  },
  
  {
    title: '申报人',
    dataIndex: 'reporterName',
    width: 220,
  },
  {
    title: '申报人部门',
    dataIndex: 'reporterDepartment',
    width: 220,
  },
  {
    title: '申报人电话',
    dataIndex: 'reporterPhone',
    width: 220,
  },
  {
    title: '申报人邮箱',
    dataIndex: 'reporterEmail',
    width: 220,
  },
  {
    title: '申报时间',
    dataIndex: 'reportingTime',
    width: 220,
  },
  {
    title: '流程类型',
    dataIndex: 'processType',
    width: 220,
  },
  {
    title: '当前指派人员ID',
    dataIndex: 'assignUserId',
    width: 220,
  },
];

/** 查询数据 */
export const searchFormSchema: FormSchema[] = [
  {
    label: '标题',
    field: 'title',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '优先级',
    field: 'priority',
    component: 'Select',
    componentProps: {
      options: dict.DicPriorityOptions,
      showSearch: true,
      optionFilterProp: 'label',
    },
    colProps: { span: 6 },
  },
  {
    label: '申报人',
    field: 'reporterName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '申报人电话',
    field: 'reporterPhone',
    component: 'Input',
    colProps: { span: 6 },
  },
];

/** 表单数据 */
export const formSchema: FormSchema[] = [
  {
    label: '主键ID',
    field: 'id',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '标题',
    field: 'title',
    component: 'Input',
    required: true,
    colProps: { span: 12 },
  },

  {
    label: '优先级',
    field: 'priority',
    component: 'Select',
    componentProps: {
      options: dict.DicPriorityOptions,
      showSearch: true,
      optionFilterProp: 'label',
    },
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '申报人',
    field: 'reporterName',
    component: 'Input',
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '申报人部门',
    field: 'reporterDepartment',
    component: 'Input',
    colProps: { span: 12 },
  },
  {
    label: '申报人电话',
    field: 'reporterPhone',
    component: 'Input',
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '申报人邮箱',
    field: 'reporterEmail',
    component: 'Input',
    colProps: { span: 12 },
  },
  {
    label: '描述',
    field: 'description',
    component: 'Input',
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      })
    },
    colProps: { span: 24 },
  },
  {
    label: '申报时间',
    field: 'reportingTime',
    component: 'DatePicker',
    required: true,
    colProps: { span: 12 },
    componentProps:{
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },

];

/** 详情数据 */
export const detailSchema: DescItem[] = [
  {
    label: '标题',
    field: 'title',
    span: 12,
  },
  {
    label: '流程类型',
    field: 'processType',
    span: 12,
  },
  {
    label: '优先级',
    field: 'priority',
    render: (val) => {
      return dictConversion(dict.DicPriorityOptions, val);
    },
    span: 12,
  },
  {
    label: '申报人',
    field: 'reporterName',
    span: 12,
  },
  {
    label: '申报人部门',
    field: 'reporterDepartment',
    span: 12,
  },
  {
    label: '申报人电话',
    field: 'reporterPhone',
    span: 12,
  },
  {
    label: '申报人邮箱',
    field: 'reporterEmail',
    span: 12,
  },
  {
    label: '申报时间',
    field: 'reportingTime',
    span: 12,
  },
  {
    label: '工单状态',
    field: 'faultProcessStatus',
    span: 12,
  },
  {
    label: '当前指派人员ID',
    field: 'assignUserId',
    span: 12,
  },  
];

export const baseFormSchema: FormSchema[] = [
  {
    label: '是否受理',
    field: 'acceptorType',
    component: 'Select',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ]
    },
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '受理用户',
    field: 'acceptorUserName',
    component: 'Input',
    colProps: { span: 12 },
  },
  {
    label: '备注',
    field: 'description',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
  {
    label: '工单流程ID',
    field: 'processId',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
];

/** 处理结果表单 */
export const resultFormSchema: FormSchema[] = [
  {
    label: '主键ID',
    field: 'id',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '故障管理ID',
    field: 'processId',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '原因分析',
    field: 'causeAnalysis',
    component: 'InputTextArea',
    required: true,
    colProps: { span: 24 },
  },
  {
    label: '影响业务',
    field: 'businessImpact',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
  {
    label: '解决方案',
    field: 'solution',
    component: 'InputTextArea',
    required: true,
    colProps: { span: 24 },
  },
  {
    label: '最终结果',
    field: 'resolutionResult',
    component: 'Input',
    required: true,
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      })
    },
    colProps: { span: 24 },
  },
  {
    label: '解决时间',
    field: 'resolutionTime',
    component: 'DatePicker',
    required: true,
    colProps: { span: 12 },
    componentProps:{
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
  {
    label: '受理用户',
    field: 'acceptorUser',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '受理用户名',
    field: 'acceptorUserName',
    component: 'Input',
    colProps: { span: 12 },
  },
]

/** 关闭工单表单 */
export const closeFormSchema: FormSchema[] = [
  {
    label: '主键ID',
    field: 'id',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '故障管理ID',
    field: 'processId',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '处理状态',
    field: 'processingStatus',
    component: 'RadioGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '关闭', value: 1 },
        { label: '重新处理', value: 0 },
      ]
    },
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '处理结果',
    field: 'processingResult',
    component: 'Select',
    defaultValue: 'success',
    componentProps: {
      options: [
        { label: '成功解决', value: 'success' },
        { label: '部分解决', value: 'part' },
        { label: '未解决', value: 'unresolved' },
        { label: '无法解决', value: 'unable' },
        { label: '无需处理', value: 'not' },
        { label: '其他', value: 'other' },
      ]
    },
    required: true,
    colProps: { span: 12 },
  },
  {
    label: '评分',
    field: 'score',
    component: 'Rate',
    colProps: { span: 12 },
  },
  {
    label: '评价内容',
    field: 'content',
    component: 'Input',
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      })
    },
    colProps: { span: 24 },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
  {
    label: '关闭时间',
    field: 'closeTime',
    component: 'DatePicker',
    colProps: { span: 12 },
    componentProps:{
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
  {
    label: '受理用户',
    field: 'acceptorUser',
    component: 'Input',
    show: false,
    colProps: { span: 12 },
  },
  {
    label: '受理用户名',
    field: 'acceptorUserName',
    component: 'Input',
    show: true,
    colProps: { span: 12 },
  },
];


/** 受理情况:详情数据 */
export const detailDisposeSchema: DescItem[] = [
  {
    label: '是否受理',
    field: 'acceptorType',
    span: 12,
    render: (val) => {
      return val === '1' ? h(Tag, { color: 'green' }, '是') : h(Tag, { color: 'red' }, '否');
    }
  },
  {
    label: '受理用户名',
    field: 'acceptorUserName',
    span: 12,
  },
  {
    label: '受理时间',
    field: 'createTime',
    span: 12,
  },
  {
    label: '备注',
    field: 'description',
    span: 24,
  }
];

export const detailResultSchema: DescItem[] = [
  {
    label: '原因分析',
    field: 'causeAnalysis',
    span: 24,
  },
  {
    label: '影响业务',
    field: 'businessImpact',
    span: 24,
  },
  {
    label: '解决方案',
    field: 'solution',
    span: 24,
  },
  {
    label: '最终结果',
    field: 'resolutionResult',
    span: 24,
  },
  {
    label: '解决时间',
    field: 'resolutionTime',
    span: 12,
  },
  {
    label: '受理用户',
    field: 'acceptorUserName',
    span: 12,
  }
];

export const detailCloseSchema: DescItem[] = [
  {
    label: '处理状态',
    field: 'processingStatus',
    span: 12,
    render: (val) => {
      return val === '1' ? h(Tag, { color: 'green' }, '关闭') : h(Tag, { color: 'red' }, '重新处理');
    }
  },
  {
    label: '处理结果',
    field: 'processingResult',
    render: (val) => {
      var result = '';
      if(val === 'success'){
        result = '成功解决';
      }else if(val === 'part'){
        result = '部分解决';    
      }else if(val === 'unresolved'){
        result = '未解决';  
      }else if(val === 'unable'){
        result = '无法解决';  
      }else if(val === 'not'){
        result = '无需处理';
      }else if(val === 'other'){
        result = '其他';  
      }
      return result;
    },
    span: 12,
  },
  {
    label: '评分',
    field: 'score',
    span: 12,
  },
  {
    label: '评价内容',
    field: 'content',
    span: 24,
  },
  {
    label: '关闭时间',
    field: 'closeTime',
    span: 12,
  },
  {
    label: '受理用户',
    field: 'acceptorUserName',
    span: 12,
  },
  {
    label: '备注',
    field: 'remark',
    span: 24,
  },
  
];